package com.surelution.cppei.weixin.gate

import org.springframework.dao.DataIntegrityViolationException

import com.surelution.cppei.card.CardInfo;

class SubscriberGascardController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	def cardService

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [subscriberGascardInstanceList: SubscriberGascard.list(params), subscriberGascardInstanceTotal: SubscriberGascard.count()]
    }

    def create() {
        [subscriberGascardInstance: new SubscriberGascard(params)]
    }

    def save() {
        def subscriberGascardInstance = new SubscriberGascard(params)
        if (!subscriberGascardInstance.save(flush: true)) {
            render(view: "create", model: [subscriberGascardInstance: subscriberGascardInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), subscriberGascardInstance.id])
        redirect(action: "show", id: subscriberGascardInstance.id)
    }

    def show(Long id) {
        def subscriberGascardInstance = SubscriberGascard.get(id)
        if (!subscriberGascardInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), id])
            redirect(action: "list")
            return
        }

        [subscriberGascardInstance: subscriberGascardInstance]
    }

    def edit(Long id) {
        def subscriberGascardInstance = SubscriberGascard.get(id)
        if (!subscriberGascardInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), id])
            redirect(action: "list")
            return
        }

        [subscriberGascardInstance: subscriberGascardInstance]
    }

    def update(Long id, Long version) {
        def subscriberGascardInstance = SubscriberGascard.get(id)
        if (!subscriberGascardInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (subscriberGascardInstance.version > version) {
                subscriberGascardInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'subscriberGascard.label', default: 'SubscriberGascard')] as Object[],
                          "Another user has updated this SubscriberGascard while you were editing")
                render(view: "edit", model: [subscriberGascardInstance: subscriberGascardInstance])
                return
            }
        }

        subscriberGascardInstance.properties = params

        if (!subscriberGascardInstance.save(flush: true)) {
            render(view: "edit", model: [subscriberGascardInstance: subscriberGascardInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), subscriberGascardInstance.id])
        redirect(action: "show", id: subscriberGascardInstance.id)
    }

    def delete(Long id) {
        def subscriberGascardInstance = SubscriberGascard.get(id)
        if (!subscriberGascardInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), id])
            redirect(action: "list")
            return
        }

        try {
            subscriberGascardInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'subscriberGascard.label', default: 'SubscriberGascard'), id])
            redirect(action: "show", id: id)
        }
    }
	
	def mobileShow(Long id) {
		def subscriber = Subscriber.get(id)
		if(subscriber) {
			def cards = SubscriberGascard.findAllBySubscriber(subscriber)
			[cards:cards, subscriber:subscriber]
		}
	}
	
	def addCard(Long id) {
		def subscriber = Subscriber.get(id)
		[subscriber:subscriber]
	}
	
	def saveCard() {
		
		def cardNo = params.cardNo
		def certificateNo = params.certificateNo
		def subscriberId = params['subscriber.id']
		def subscriber = Subscriber.get(subscriberId)
		
		if(!cardNo || !certificateNo) {
			flash.message = "请输入卡号和证件号"
			redirect(action:'mobileShow', id:subscriber.id)
			return
		}
		
		def subscriberGascardInstance = SubscriberGascard.findBySubscriberAndCardNo(subscriber, cardNo)
		if(!subscriberGascardInstance) {
			subscriberGascardInstance = new SubscriberGascard()
		}
		if(subscriberGascardInstance.certificated) {
			flash.message = "此卡已经绑定成功，无需再次邦卡"
			redirect(action:'mobileShow', id:subscriberGascardInstance.subscriber.id)
			return
		}
		subscriberGascardInstance.cardNo = cardNo
		subscriberGascardInstance.subscriber = subscriber
		subscriberGascardInstance.certificateNo = certificateNo
		
		if(cardService.validateCard(subscriberGascardInstance)) {
			redirect(action:'mobileShow', id:subscriberGascardInstance.subscriber.id)
			return
		}
        if (!subscriberGascardInstance.save(flush: true)) {
            render(view: "create", model: [subscriberGascardInstance: subscriberGascardInstance])
            return
        }

        redirect(action: "show", id: subscriberGascardInstance.id)
    }
	
	def certificatedCards(Long id) {
		def subscriber = Subscriber.get(id)
		if(subscriber) {
			def cards = SubscriberGascard.findAllBySubscriberAndCertificated(subscriber, true)
			[cards:cards, subscriber:subscriber]
		}
	}
	
	def cardInfo(Long id) {
		def card = SubscriberGascard.get(id)
		if(card) {
			def cardType = card.cardType
			if(cardType) {
				def calssName = cardType.queryClassName
				CardInfo ci = this.class.classLoader.loadClass(calssName).newInstance()
				ci = ci.query(card.cardNo)
				[cardInfo:ci]
			}
		}
	}
}
